﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile.Nodes
{
	public class Return : Executable
	{
		public Expression Value { get; private set; }

		public Return(Expression expression)
		{
			this.Value = expression ?? NullConstant.Instance;
		}

		public static Executable ParseReturn(Tokens tokens)
		{
			tokens.PopExpected("return");

			if (tokens.PopIfPresent(";"))
			{
				return new Return(NullConstant.Instance);
			}

			Expression expression = Expression.Parse(tokens);
			tokens.PopExpected(";");

			return new Return(expression);
		}

		public override void ResolveCode(Class c, Dictionary<object, object> parents)
		{
			if (this.Value != null)
			{
				this.Value.ResolveCode(c, parents);
			}
		}

		public override void ResolveClassToThis(Class c)
		{
			if (this.Value != null)
			{
				this.Value.ResolveClassToThis(c);
			}
		}
	}
}
